package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IPayMentLockDao;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
public class PayMentLockDaoImpl extends BaseDaoImpl implements IPayMentLockDao  {
    @Override
    public void batchSavePayMentLock(List<Map<String, Object>> payMentLocks,String province) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Object[]> paramList = new ArrayList<>(payMentLocks.size());
        for(Map<String, Object> map: payMentLocks){
            Object[] paramArr = {
                    SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()), String.valueOf(DateUtils.getCurrentTimeStamp()),
                    map.get("agency_id"), map.get("bgt_id"), map.get("payee_acct_name"),
                    map.get("payee_acct_no"), map.get("payee_acct_bank_name"), map.get("amt"),
                    "1", 0,province
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));

            paramList.add(params.toArray());
        }
        StringBuilder sb = new StringBuilder();
        sb.append(" insert into ").append(TableName.PAY_MENT_LOCK)
                .append(" ( id, create_time, update_time, " )
                .append("agency_id, bgt_id, payee_acct_name, " )
                .append("payee_acct_no, payee_acct_bank_name, amt,")
                .append(" is_lock, is_deleted,mof_div_code ) ")
                .append(" values (?,?,?,")
                .append("?,?,?,")
                .append("?,?,?,")
                .append("?,?,?) ");
        super.getJdbcTemplate().batchUpdate(sb.toString(),  new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList.get(i);
                for(int a = 0; a < 12; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public void batchPhysicalDeletePayMentLock(List<String> agencyIds) {
        if(!CollectionUtils.isEmpty(agencyIds)){
        StringBuilder sb = new StringBuilder();
        sb.append(" delete from ").append(TableName.PAY_MENT_LOCK)
                .append(" where ").append(SysUtils.inSql("agency_id", agencyIds));
            super.getjdbcTemplate().update(sb.toString());
        }

    }

    @Override
    public void batchUpdatePayMentLockByBgtId(List<String> bgtIds) {
        if(!CollectionUtils.isEmpty(bgtIds)){
            StringBuilder sb = new StringBuilder();
            sb.append(" update  ").append(TableName.PAY_MENT_LOCK)
                    .append(" set is_lock=0 ")
                    .append(" where ").append(SysUtils.inSql("bgt_id", bgtIds));
            super.getjdbcTemplate().update(sb.toString());
        }
    }

    @Override
    public List<Map<String, Object>> selectPayMentLock(List<String> agencyIds) {
        StringBuilder sb = new StringBuilder();
        List<Object> conditions = new ArrayList<>();
        sb.append(" select ")
                .append(" bgt_id as \"bgt_id\", payee_acct_name as \"payee_acct_name\", payee_acct_no as \"payee_acct_no\",")
                .append(" payee_acct_bank_name as \"payee_acct_bank_name\",amt as \"amt\",agency_id as \"agency_id\" ")
                .append(" from ")
                .append(TableName.PAY_MENT_LOCK)
                .append(" where ").append(SysUtils.inSql("agency_id", agencyIds)).append(" and is_lock=1 ");
        return super.getjdbcTemplate().queryForList(sb.toString(),conditions.toArray());
    }
}
